//
//  DetailViewController.m
//  宝宝儿歌
//
//  Created by Clement_Gu on 16/4/20.
//  Copyright © 2016年 baobaoerge. All rights reserved.
//

#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
@interface DetailViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,assign)BOOL camera;
@property (nonatomic,strong)UIButton *userImageButton;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    
    //创建界面
    [self buildAllControls];
}

-(void)buildAllControls
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//    NSMutableDictionary *dic = [def objectForKey:@"myInfo"];
//    UIImage *userPhoto = dic[@"userPhoto"];
//    NSString *userName = dic[@"userName"];
//    NSString *userSignature = dic[@"userSignature"];
    UIImage *image = [[UIImage alloc]init];
    if ([[def objectForKey:@"userPhoto"] isEqualToString:@"placeholder"]) {
        image = [UIImage imageNamed:@"placeholder"];
    }
    else
    {
       image = [UIImage imageWithData:[def objectForKey:@"userPhoto"]];
    }
    
    
    CGFloat width = self.view.bounds.size.width;
    CGFloat height = self.view.bounds.size.height;
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 30, 50, 50)];
    [btn1 setTitle:@"返回" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn = [[UIButton alloc]init];
    [btn setImage:image forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(addPhoto) forControlEvents:UIControlEventTouchUpInside];
    btn.layer.borderWidth = 0.5;
    btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    btn.frame = CGRectMake(width*0.5-50, 30, 100, 100);
    self.userImageButton = btn;
    [self.view addSubview:btn];
    
    
    UITextField *txtName = [[UITextField alloc]init];
    txtName.text = [def objectForKey:@"userName"];
    txtName.borderStyle = UITextBorderStyleRoundedRect;
    txtName.enabled = NO;
    txtName.frame = CGRectMake(width*0.2, 150, width*0.6, 40);
//    txtName.bounds = CGRectMake(0, 0, self.view.bounds.size.width*0.66, 40);
//    txtName.center = CGPointMake(self.view.bounds.size.width*0.5, 150);
    [self.view addSubview:txtName];

    UITextView *txtSignature = [[UITextView alloc]init];
    txtSignature.text = [def objectForKey:@"userSignature"];
    txtSignature.font = [UIFont systemFontOfSize:20];
    txtSignature.editable = NO;
    txtSignature.layer.borderWidth = 0.2;
    txtSignature.layer.borderColor = [UIColor darkGrayColor].CGColor;
    txtSignature.frame = CGRectMake(0, 230, width, height-CGRectGetMaxY(txtName.frame)-40);
//    txtSignature.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-CGRectGetMaxY(txtName.frame)-10);
    [self.view addSubview:txtSignature];


}
-(void)goBack
{
    [self dismissViewControllerAnimated:YES completion:nil];

}


/**
 *  点击头像时候选择更换头像的饿数据源
 */
-(void)showAlert
{
    //判断相机是否可用 可用就用相机 不可用就用相册
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择数据源" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *elbum = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        //判断相机是否可用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            //判断设备版本 8.0以上的要指定一下属性 照相机才不会报错
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0 )
            {
                self.modalPresentationStyle = UIModalPresentationCurrentContext;
            }
            [self pickerImage:sourceType];
        }
        else
        {
            UIImagePickerControllerSourceType source = UIImagePickerControllerSourceTypePhotoLibrary;
            [self pickerImage:source];
        }
        
    }];
    UIAlertAction *photo = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self pickerImage:sourceType];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:elbum];
    [alert addAction:photo];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)pickerImage:(UIImagePickerControllerSourceType)sourceType
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;//设置可编辑
    imagePicker.sourceType = sourceType;
    [self presentViewController:imagePicker animated:YES completion:nil];


}
-(void)addPhoto
{
    [self showAlert];
}
#pragma mark imagePikerController Delegate
//点击use 事件
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    
    NSURL *imageURL = [editingInfo valueForKey:UIImagePickerControllerReferenceURL];
    
    NSData *data = [NSData dataWithContentsOfURL:imageURL];
    
//    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
//    {
//        ALAssetRepresentation *representation = [myasset defaultRepresentation];
//        NSString *fileName = [representation filename];
//        NSLog(@"fileName : %@",fileName);
//    };
//    
//    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
//    [assetslibrary assetForURL:imageURL
//                   resultBlock:resultblock
//                  failureBlock:nil];
    
    
    
    
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    [def setObject:data forKey:@"userPhoto"];
//    NSMutableDictionary *dic = [def objectForKey:@"myInfo"];
//    [dic setObject:image forKey:@"userPhoto"];
//    [def setObject:dic forKey:@"myInfo"];
    [def synchronize];
    [self.userImageButton setImage:image forState:UIControlStateNormal];
//    [self.userImageButton.imageView sd_setImageWithURL:imageURL];
    [self dismissViewControllerAnimated:YES completion:nil];
}

//点击取消的事件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
